﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Linq.Expressions;
using System.Reflection;

namespace MockFramework.Tests
{
    [TestFixture]
    public class ExpressionsFixture
    {
        [Test]
        public void ExpressionTest1()
        {
            Expression<Func<Int32, Int32, Int32>> expression1 = (a, b) => a + b;
            MethodInfo mi1 = ((BinaryExpression) expression1.Body).Method;
            Console.WriteLine(expression1.Compile().Invoke(3, 4));

            Expression<Func<String, Boolean>> expression2 = value => value.Length <= 3;
            MethodInfo mi2 = ((BinaryExpression)expression2.Body).Method;
            Console.WriteLine(expression2.Compile().Invoke("Três")
                                  ? "Três tem 3 caracteres ou menos"
                                  : "Três tem mais de 3 caracteres");

            Expression<Func<Order, bool>> expression3 = x => x.IsFilled;
            MethodInfo mi3 = ((MethodCallExpression)expression3.Body).Method;
            Console.WriteLine(mi3.GetMethodBody());
        }
    }

    public interface IWarehouse
    {
        bool HasInventory(string productName,
        int quantity);
        void Remove(string productName,
        int quantity);
    }

    public class Order
    {
        public string ProductName { get; private set; }
        public int Quantity { get; private set; }
        public bool IsFilled { get; private set; }
        public Order(string productName, int quantity)
        {
            this.ProductName = productName;
            this.Quantity = quantity;
        }
        public void Fill(IWarehouse warehouse)
        {
            if (warehouse.HasInventory(ProductName,
            Quantity))
            {
                warehouse.Remove(ProductName, Quantity);
                IsFilled = true;
            }
        }
    }
}
